package com.chukun.spring.dependency.injection;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * 基于 {@link org.springframework.beans.factory.Aware} 实现aware接口的方式注入示例
 * @author chukun
 */
public class AwareInterfaceInjectionDemo implements BeanFactoryAware, ApplicationContextAware {

    private static BeanFactory beanFactory;

    private static ApplicationContext applicationContext;

    public static void main(String[] args) {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();

        applicationContext.register(AwareInterfaceInjectionDemo.class);

        // 启动 Spring 应用上下文
        applicationContext.refresh();

        System.out.println(AwareInterfaceInjectionDemo.beanFactory == applicationContext.getBeanFactory());

        System.out.println(AwareInterfaceInjectionDemo.applicationContext == applicationContext);

        // 显示地关闭 Spring 应用上下文
        applicationContext.close();

    }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        AwareInterfaceInjectionDemo.beanFactory = beanFactory;
    }


    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        AwareInterfaceInjectionDemo.applicationContext = applicationContext;
    }
}
